package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-02
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        NursingPlan plan = getById( id);
        NursingPlanVo planVo = new NursingPlanVo();
        BeanUtils.copyBeanProp(planVo,plan);

        LambdaQueryWrapper<NursingProjectPlan> qw = new LambdaQueryWrapper<>();
        qw.eq(NursingProjectPlan::getPlanId,id);
        List<NursingProjectPlan> nursingProjectPlans = this.nursingProjectPlanMapper.selectList(qw);

        List<NursingProjectPlanVo> voList = nursingProjectPlans.stream().map(nursingProjectPlan -> {
            NursingProjectPlanVo vo = new NursingProjectPlanVo();
            BeanUtils.copyBeanProp(vo,nursingProjectPlan);
            vo.setProjectId(String.valueOf(nursingProjectPlan.getProjectId()));
            vo.setExecuteCycle(String.valueOf(nursingProjectPlan.getExecuteCycle()));
            return vo;
        }).collect(Collectors.toList());
        planVo.setProjectPlans(voList);
        return planVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto dto)
    {
        //新增护理计划
        //使用工具类实现dto->entity
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto,nursingPlan);//拷贝非空属性 名称类型要一样
//        int count = this.nursingPlanMapper.insertNursingPlan(nursingPlan);
        int count = this.nursingPlanMapper.insert(nursingPlan);

        //新增护理计划和护理项目的关联关系数据
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        for(NursingProjectPlan projectPlan:projectPlans){
            projectPlan.setPlanId(nursingPlan.getId());
            this.nursingProjectPlanMapper.insert(projectPlan);
        }
        return count;
    }

    /**
     * 修改护理计划
     * 
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto dto)
    {
        //修改护理计划
        //使用工具类实现dto->entity
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyBeanProp(nursingPlan,dto);
        int num = this.nursingPlanMapper.updateById(nursingPlan);
        //修改护理计划对应的护理项目数据【先删除再新增】
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        if(!CollectionUtils.isEmpty(projectPlans)){
            //根据护理计划id，删除该护理计划对应的护理项目数据
            this.nursingProjectPlanMapper.delete(
                    new LambdaQueryWrapper<NursingProjectPlan>()
                            .eq(NursingProjectPlan::getPlanId,nursingPlan.getId()));
            //批量新增
            for(NursingProjectPlan projectPlan:projectPlans){
                projectPlan.setPlanId(nursingPlan.getId());
                projectPlan.setUpdateTime(LocalDateTime.now());
                projectPlan.setUpdateBy(nursingPlan.getUpdateBy());
                this.nursingProjectPlanMapper.insert(projectPlan);
            }
        }
        return num;
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        int num = this.nursingPlanMapper.deleteNursingPlanByIds(ids);

        this.nursingProjectPlanMapper.delete(new LambdaQueryWrapper<NursingProjectPlan>()
                .in(NursingProjectPlan::getPlanId,ids));

        return num;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
                return removeById(id) ? 1 : 0;
    }
}
